package com.wlyuan.open.application.listener;

import com.wlyuan.core.domain.event.DomainEventListener;
import com.wlyuan.open.OpenBaseSyncDTO;
import com.wlyuan.open.domain.source.SourceCache;
import com.wlyuan.open.domain.source.event.cache.OpenCacheRelation;
import com.wlyuan.open.domain.source.repository.cache.*;
import com.wlyuan.open.dto.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.greenrobot.eventbus.Subscribe;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author lijing
 * @description
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class OpenCacheRelationEventListener implements DomainEventListener<OpenCacheRelation> {
    private final OpenTenantCacheRepository openTenantCacheRepository;
    private final OpenCustomerCacheRepository openCustomerCacheRepository;
    private final OpenUserCacheRepository openUserCacheRepository;
    private final OpenConsigneeCacheRepository openConsigneeCacheRepository;
    private final OpenGoodsCacheRepository openGoodsCacheRepository;
    private final OpenCarrierCacheRepository openCarrierCacheRepository;
    private final OpenOrderCacheRepository openOrderCacheRepository;
    private final OpenDriverCacheRepository openDriverCacheRepository;
    private final OpenTransportCacheRepository openTransportCacheRepository;
    private final OpenTemplateCacheRepository openTemplateCacheRepository;
    private final OpenBillCacheRepository openBillCacheRepository;
    private final OpenCombineCacheRepository openCombineCacheRepository;

    @Override
    @Subscribe
    public void handle(OpenCacheRelation event) {
        var sourceId = event.getSourceId();
        Optional.ofNullable(sourceId).ifPresent(sId -> convert(event.getSource(), sId, event.getTarget()));
    }

    private <T extends OpenBaseSyncDTO> void convert(T source, String sourceId, SourceCache target) {
        if (source instanceof OpenTenantSyncDTO) {
            openTenantCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenCustomerSyncDTO) {
            openCustomerCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenUserSyncDTO) {
            openUserCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenConsigneeSyncDTO) {
            openConsigneeCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenGoodsSyncDTO) {
            openGoodsCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenOrderSyncDTO) {
            openOrderCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenDriverSyncDTO) {
            openDriverCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenCarrierSyncDTO) {
            var carrier = (OpenCarrierSyncDTO) source;
            openCarrierCacheRepository.save(carrier.getSourceKey(), target);
        }
        if (source instanceof OpenTransportSyncDTO) {
            openTransportCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenCombineSyncDTO) {
            openCombineCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenTemplateSyncDTO) {
            openTemplateCacheRepository.save(Long.valueOf(sourceId), target);
        }
        if (source instanceof OpenBillSyncDTO) {
            openBillCacheRepository.save(Long.valueOf(sourceId), target);
        }
    }

}
